package com.sixuerain.heartkey.prayer.db;

import android.content.Context;
import android.database.Cursor;
import com.sixuerain.heartkey.prayer.db.BaseDAO;
import com.sixuerain.heartkey.prayer.db.model.LectionModel;
import com.sixuerain.heartkey.prayer.db.table.LectionTable;
import com.sixuerain.heartkey.prayer.tools.Util;

public class LectionDAO extends BaseDAO<LectionModel> {

	public LectionDAO(Context context) {
		super(context);
	}

	@Override
	public LectionModel mapRow(Cursor cursor, int rowNum) {
		LectionModel lectionModel = new LectionModel();
		lectionModel.setContent(cursor.getString(
				cursor.getColumnIndex(LectionTable.CONTENT)));
		lectionModel.setLectionId(cursor.getInt(
				cursor.getColumnIndex(LectionTable.ID)));
		lectionModel.setNotes(cursor.getString(
				cursor.getColumnIndex(LectionTable.NOTES)));
		lectionModel.setTitle(cursor.getString(
				cursor.getColumnIndex(LectionTable.TITLE)));
		lectionModel.setPronounce(cursor.getString(
				cursor.getColumnIndex(LectionTable.PRONOUNCE)));
		lectionModel.setName(cursor.getString(
				cursor.getColumnIndex(LectionTable.NAME)));
		lectionModel.setShortName(cursor.getString(
				cursor.getColumnIndex(LectionTable.SHORTNAME)));
		lectionModel.setLongName(cursor.getString(
				cursor.getColumnIndex(LectionTable.LONGNAME)));
		return lectionModel;
	}
	
	public boolean isExistLection(LectionModel lecModel) {
		String lecId = String.valueOf(Util.generateLectionID(lecModel));
		LectionModel dbLecModel = this.singleSearch(LectionTable.TABLE_NAME, 
				null, LectionTable.ID+"=?", 
				new String []{lecId}, 
				null, null, null, "1");
		if(dbLecModel == null || dbLecModel.getLectionId()==0)
			return false;
		return true;
	}

}
